package org.sction.manager.database.proxool;

import java.io.File;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public class ProxoolListener implements ServletContextListener {
	private static final String XML_FILE_PROPERTY = "xmlFile";

	private static final String PROPERTY_FILE_PROPERTY = "propertyFile";
	private static Logger log = Logger.getLogger(ProxoolListener.class);
	private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";

	private boolean autoShutdown = true;

	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent arg0) {
		ServletContext context = arg0.getServletContext(); // 对应servlet的init方法中ServletConfig.getServletContext()
		String appDir = arg0.getServletContext().getRealPath("/");
		Properties properties = new Properties();
		Enumeration<?> names = context.getInitParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			String value = context.getInitParameter(name);
			if (name.equals(XML_FILE_PROPERTY)) {
				try {
					File file = new File(value);
					if (file.isAbsolute()) {
						JAXPConfigurator.configure(value, autoShutdown);
					} else {
						JAXPConfigurator.configure(appDir + File.separator
								+ value, autoShutdown);
					}
				} catch (ProxoolException e) {
					e.printStackTrace();
				}
			} else if (name.equals(PROPERTY_FILE_PROPERTY)) {

				File file = new File(value);
				if (file.isAbsolute()) {
					PropertyConfigurator.configure(value);
				} else {
					PropertyConfigurator.configure(appDir + File.separator
							+ value);
				}
			} else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
				autoShutdown = Boolean.valueOf(value).booleanValue();
			} else if (name.startsWith("jdbc")) { // 此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样
				properties.setProperty(name, value);
			}
		}

		if (properties.size() > 0) {
			PropertyConfigurator.configure(properties);
		}
		log.info("proxool启动.");
	}

}
